#include "MPX_SUPT.H"
#include "COMMANDH.H"
#include "R2.H"
#include "R3.h"
#include <string.h>
#include <stdlib.h>

	
void command_handler()
{
	
	
	char PROMPT_SYMBOL[20] = ":)";

	int sys_req_error; // used to grab error or size from sys_req
	int done = 0; //used to end the while loop
	char input_buffer [80]; //contains the full string inputted by the user
	int size_of_buffer = 80; //size of above input_buffer for reiteration each loop

	char space[] = " "; //used in the finction strtok() to seperate functions and parameters
	char *input_word = NULL; //used by strtok() to hold the current word while parsing on the spaces
	int switch_flag = 0; //used by the switch statment to count the words of input
	char command[80]; //used by the switch statment to hold the first inputed word
	char argument_1[80]; //used by the switch statment to hold the second inputted word
	char argument_2[80]; //used by the switch statment to hold the third inputted word
	char argument_3[80]; //used by the switch statment to hold the fourth inputted word
	char argument_4[80]; //used by the switch statment to hold the fifth inputted word
	char argument_5[80]; //used by the switch statment to hold the sixth inputted word

	while(!done)//
	{
		printf("%s", PROMPT_SYMBOL); //prompt display

		size_of_buffer = 80; //reset to max size before a new read
		sys_req_error = sys_req(READ, TERMINAL, input_buffer, &size_of_buffer);
		if (sys_req_error < 0)
			printf("There was a problem with sys_req, error #%s\0", sys_req_error);
		else if (sys_req_error >0)
		{
			input_buffer[sys_req_error - 1]= '\0';
			input_word = strtok(input_buffer, space);
			command[0] = '\0';
			argument_1[0] = '\0';
			argument_2[0] = '\0';
			argument_3[0] = '\0';
			argument_4[0] = '\0';
			argument_5[0] = '\0';
			switch_flag = 0;
			while(input_word != NULL)
			{
				switch (switch_flag)
				{
					case 0:
						strcpy(command, input_word);
						break;
					case 1:
						strcpy(argument_1, input_word);
						break;
					case 2:
						strcpy(argument_2, input_word);
						break;
					case 3:
						strcpy(argument_3, input_word);
						break;
					case 4:
						strcpy(argument_4, input_word);					
						break;
					case 5:
						strcpy(argument_5, input_word);
						break;
					default:
						printf("we are only set up to allow five arguments on a command\n");
				}
				switch_flag ++;
				input_word = strtok(NULL, space);				
			}
			
			
			
			if(strcmp(command,"\0") ==0)
			{
				//nothing
			}
			else if(strcmp(command,"help") == 0)
			{
				help(argument_1, argument_2);
			}
			else if(strcmp(command,"date") == 0)
			{
				date(argument_1, argument_2);
			}
			else if(strcmp(command, "directory") == 0)
			{
				directory(argument_1, argument_2);
			}
			else if(strcmp(command, "version") == 0)
			{
				version(argument_1);
			}
			else if(strcmp(command, "terminate") == 0)
			{
				terminate(argument_1);
			}
			else if(strcmp(command, "create") == 0)
			{
				create_pcb(argument_1, argument_2, argument_3, argument_4);
			}
			else if(strcmp(command, "delete") == 0)
			{
				delete_pcb(argument_1, argument_2);
			}
			else if(strcmp(command, "block") == 0)
			{
				block_pcb(argument_1, argument_2);
			}
			else if(strcmp(command, "unblock") == 0)
			{
				unblock_pcb(argument_1, argument_2);
			}
			else if(strcmp(command, "suspend") == 0)
			{
				suspend_pcb(argument_1, argument_2);
			}
			else if(strcmp(command, "resume") == 0)
			{
				resume_pcb(argument_1, argument_2);
			}
			else if(strcmp(command, "priority") == 0)
			{
				set_priority_pcb(argument_1, argument_2, argument_3);
			}
			else if(strcmp(command, "show") == 0)
			{
				show_pcb(argument_1, argument_2);
			}
			else if(strcmp(command, "showall") == 0){
				show_all(argument_1);
			}
			else if(strcmp(command, "showr") == 0){
				show_ready(argument_1);
			}
			else if(strcmp(command, "showb") == 0){
				show_blocked(argument_1);
			}
			else if(strcmp(command, "showsr") == 0){
				show_suspended_ready(argument_1);
			}
			else if(strcmp(command, "showsb") == 0){
				show_suspended_blocked(argument_1);
			}
			else if(strcmp(command, "dispatch") == 0){
				dispatch();
			}
			else if(strcmp(command, "load") == 0){
				load_procs(argument_1);
			}
			else if(strcmp(command, "clear") == 0)
			{
				if(argument_1[0] == '\0')
				{
					sys_req(CLEAR, TERMINAL, 0, 0);
				}
				else
				{
					printf("The clear command cannot use that argument, type 'help' for help\n");
				}
			}
			else if(strcmp(command, "prompt") == 0 && argument_1[0] != '\0')
			{
				if(argument_2[0] == '\0')
				{
					strcpy(PROMPT_SYMBOL, argument_1);
				}
				else 
				{
					printf("The prompt command cannot use that argument, type 'help' for help\n");
				}
			}
			else
			{
				printf("%s is not a valid command or was not used correctly, type \'help\' for help\n", command);
			}
			
		}
		else
		{
			// sys_req_error = 0 means there was no error, but also no input;
			printf("\n%s", PROMPT_SYMBOL);
		}
	}
}